package com.hushan.elevator.minio;

import com.hushan.elevator.model.DeviceInfo;
import com.hushan.elevator.model.RiskRecord;
import com.hushan.elevator.utils.MinioUtils;
import com.hushan.elevator.utils.UploadUtils;
import io.minio.*;
import io.minio.errors.MinioException;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Objects;

import static org.junit.jupiter.api.Assertions.assertTrue;

@Slf4j
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class test {
    @Resource
    private MinioClient minioClient;
    @Resource
    private MinioUtils minioUtils;
    @Value("${spring.minio.bucket.riskImg}")
    private String riskBucket;
    private final String bucketName = "test";
    File file = new File("D:/test/test.jpg");

    @Test
    void createBucket() throws Exception {
        if (!minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build())) {
            minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
        }
    }

    @Test
    void deleteBucket() throws Exception {
        if (minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build())) {
            minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build());
        }
    }

    @Test
    void bucketExists() throws Exception {
        boolean exists = minioClient.bucketExists(
                BucketExistsArgs.builder().bucket(bucketName).build());
        System.out.println(exists);
    }


    @Test
    void fileUpload() throws Exception {
        FileInputStream inputStream = new FileInputStream(file);
        minioClient.putObject(PutObjectArgs.builder()
                .bucket(bucketName)
                .object(file.getName())
                .stream(inputStream, file.length(), -1)
                .contentType(MediaType.IMAGE_JPEG_VALUE)
                .build());
    }

    @Test
    void fileDelete() throws Exception {
        minioClient.removeObject(
                RemoveObjectArgs.builder()
                        .bucket(bucketName)
                        .object(file.getName())
                        .build());
    }

    @Test
    void fileDownload() throws Exception {
        try (InputStream stream = minioClient.getObject(
                GetObjectArgs.builder()
                        .bucket(riskBucket)
                        .object("AD014B61F50F5D1E21A10AC8" + "/" +"2024-12-16_12-18-31_2.jpg")
                        .build())) {
            // Save the file locally or process the stream as needed
            Files.copy(stream, Paths.get("D:/downloaded_" + file.getName()));
        }
    }

    @Test
    void fileExists() throws Exception {
        try {
            StatObjectResponse response = minioClient.statObject(
                    StatObjectArgs.builder()
                            .bucket(riskBucket)
                            .object("AD014B61F50F5D1E21A10AC8" + "/" +"2024-12-16_12-18-31_2.jpg")
                            .build());
            System.out.println(response);
        } catch (MinioException e) {
            System.out.println(e.getMessage());
        }
    }


    @Test
    void upload() throws Exception {
        String origin = "2024-11-25_17-12-19_2.jpg";
        String eventId = Objects.requireNonNull(origin).substring(0, origin.lastIndexOf('.'));
//        RiskRecord byEventId = riskRecordService.findByEventId(eventId);
        File file = new File("D:\\work\\project\\study\\code\\elevator\\src\\main\\resources\\static\\AD014B61F50F5D1E21A10AC8\\2024-11-25_17-12-19_2.jpg");
        try (FileInputStream inputStream = new FileInputStream(file)) {
            byte[] bytes = new byte[inputStream.available()];
            MultipartFile fileb = new MockMultipartFile("name", file.getName(), MediaType.IMAGE_JPEG_VALUE, bytes);
            minioUtils.upload(fileb, "AD014B61F50F5D1E21A10AC8" + "/" + fileb.getOriginalFilename(), riskBucket);
        }
    }

    @Test
    void get() throws Exception {
        try {
            ResponseEntity<byte[]> download = minioUtils.download("devid" + "/test.jpg", riskBucket);
            byte[] body = download.getBody();
            String firPath = "D:/test2/";
            // parse multipart
            String filename = "test.jpg";
            // save
            Path path = Paths.get(firPath + "/" + filename);
            assert body != null;
            Files.write(path, body);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
